<script type="text/javascript">
	function AdminManageCtrl(CtrlUtil, $http, $element, $injector, $q, modalService, $scope, toaster) {
		var ctrl = this;
		ctrl.pageNumber = 0;
		CtrlUtil.build(ctrl, ':root/admin/query')
		ctrl.typeMap = {
			1 : '院系管理员',
			2 : '学校管理员',
			3 : '系统管理员'
		}
		$http.post(':root/department/queryByStatusAndType', {
			status : 1,
			type : 1
		}).success(function(r) {
			ctrl.departments = r
			ctrl.depMap = {}
			angular.forEach(r, function(v) {
				ctrl.depMap[v.id] = v
			})
			return r;
		})
		this.dialogAdd = function() {
			modal({
				type : 2,
				status : 1
			}, '新增管理员')
		}
		this.update = function(admin) {
			modal(admin, '修改管理员')
		}
		function modal(admin, title) {
			var tpl = $element.find('.tpl>form').clone()
			var obj = {}
			angular.extend(obj, admin)
			modalService.confirm({
				title : title,
				content : tpl,
				result : obj,
				parentCtrl : ctrl,
				departments : ctrl.departments,
				confirmText : '保存'
			}).then(function(r) {
				if (r.type == 2) {
					r.manaDep = null
				}
				$http.post(':root/admin/update', r).success(function(r) {
					toaster.pop({
						type : 'success',
						body : title + '成功',
						timeout : 800
					});
					ctrl.query()
				})
			})
		}
		this.del = function(admin) {
			modalService.confirm({
				title : '确认删除',
				content : '确定要删除管理员' + admin.name + '吗？',
				result : admin
			}).then(function(r) {
				$http.post(':root/admin/delete', {
					id : admin.id
				}).success(function(r) {
					toaster.pop({
						type : 'success',
						body : '删除成功',
						timeout : 800
					});
					ctrl.query()
				})
			})
		}
		this.changeGh = function(gh) {
			if (gh) {
				$http.post(':root/teacher/query', {
					gh : gh,
					pageSize : 5
				}).success(function(r) {
					ctrl.ghSuggest = r;
				})
			} else {
				ctrl.ghSuggest = null;
			}
		}
		this.changeName = function(name) {
			if (name) {
				$http.post(':root/teacher/query', {
					name : name,
					pageSize : 5
				}).success(function(r) {
					ctrl.nameSuggest = r;
				})
			} else {
				ctrl.nameSuggest = null;
			}
		}
	}
</script>
<!-- Search area -->
<div ng-controller="AdminManageCtrl as ctrl" ng-init="ctrl.query()">
	<!-- Search area -->
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span>
				信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal row">
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input type="text" class="form-control" placeholder="工号/姓名" ng-model="ctrl.nameQueryParam">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen class="form-control" ng-options="m.id as m.name for m in ctrl.departments" ng-model="ctrl.departmentQueryParam">
								<option value="">--管理院系--</option>
							</select>
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen disable-search="true" ng-model="ctrl.typeQueryParam" class="form-control">
								<option value="">角色</option>
								<option value="1">院系管理员</option>
								<option value="2">学校管理员</option>
							</select>
						</div>
					</div>


				</form>
			</div>
			<div class="row pull-right">
				<div class="col-md-12">
					<button ng-click="ctrl.query()" type="submit" class="btn btn-cys btn-sm">
						<span class="glyphicon glyphicon-search"></span>
						查询
					</button>
					<button ng-click="ctrl.reset()" type="reset" class="btn btn-default btn-sm">
						<span class="glyphicon glyphicon-repeat"></span>
						重置
					</button>
				</div>
			</div>
		</div>
	</div>
	<!-- Search area END -->

	<!-- operation area -->
	<button type="button" class="btn btn-cys btn-sm" ng-click="ctrl.dialogAdd()">
		<span class="glyphicon glyphicon-plus"></span>
		新增
	</button>
	<div class="ny-top-r pull-right" ng-include="':root/components/page.html'"></div>
	<!-- operation area END -->


	<!-- data grid area -->
	<div style="padding-top: 10px;">
		<table class="table table-bordered table-hover table-striped">
			<tr>
				<td>工号</td>
				<td>姓名</td>
				<td>角色</td>
				<td>管理院系</td>
				<td>状态</td>
				<td class="text-center">操作</td>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td>{{item.employeeNo}}</td>
				<td>{{item.name}}</td>
				<td>{{ctrl.typeMap[item.type]}}</td>
				<td>{{item.type==1?ctrl.depMap[item.manaDep].name:''}}</td>
				<td>{{item.status==0?'注销':'正常'}}</td>
				<td class="text-center">
					<a ng-disabled="entity.type==3" ng-click="ctrl.update(item)" href="javascript:;" class="  ">编辑</a>
					<a ng-disabled="entity.type==3" ng-click="ctrl.del(item)" href="javascript:;" class="  ">删除</a>
				</td>
			</tr>
		</table>
		<p>
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span>
			条
		</p>
	</div>
	<!-- data grid area END -->

	<div ng-non-bindable class="tpl hidden">
		<form class="form-horizontal" style="margin-top: 30px; margin-bottom: 40px;">
			<div class="form-group">
				<label class="col-md-2 control-label">工号:</label>
				<div class="col-md-4">
					<input class="form-control" data-toggle="dropdown" ng-change="parentCtrl.changeGh(result.employeeNo)" required="required"
						placeholder="工号" type="text" ng-model="result.employeeNo">
					<ul class="dropdown-menu  open" ng-show="parentCtrl.ghSuggest.content.length>0">
						<li ng-repeat="t in parentCtrl.ghSuggest.content">
							<a href="javascript:;" ng-click="result.employeeNo=t.employeeNo;result.name=t.name;parentCtrl.ghSuggest=null;">({{t.employeeNo}}){{t.name}}</a>
						</li>
					</ul>
				</div>
				<label class="col-md-1 control-label">姓名:</label>
				<div class="col-md-4">
					<input class="form-control" data-toggle="dropdown" ng-change="parentCtrl.changeName(result.name)" required="required" placeholder="姓名"
						type="text" ng-model="result.name">
					<ul class="dropdown-menu open" ng-show="parentCtrl.nameSuggest.content.length>0">
						<li ng-repeat="t in parentCtrl.nameSuggest.content">
							<a href="javascript:;" ng-click="result.employeeNo=t.employeeNo;result.name=t.name;parentCtrl.nameSuggest=null;">({{t.employeeNo}}){{t.name}}</a>
						</li>
					</ul>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">角色:</label>
				<div class="col-md-4 radio">
					<label>
						<input type="radio" value="2" placeholder="工号*" ng-model="result.type">
						学校管理员

					</label>
					<label>
						<input type="radio" value="1" placeholder="工号*" ng-model="result.type">
						院系管理员
					</label>
				</div>
				<label class="col-md-1 control-label">状态:</label>
				<div class="col-md-4 radio">
					<label>
						<input type="radio" value="1" placeholder="工号*" ng-model="result.status">
						正常
					</label>
					<label>
						<input type="radio" value="0" placeholder="工号*" ng-model="result.status">
						注销
					</label>
				</div>
			</div>
			<div ng-if="result.type==1" class="form-group">
				<label class="col-md-2 control-label">院系:</label>
				<div class="col-md-4 ">
					<select required="required" class="form-control"   ng-options="m.id as m.name for m in departments" ng-model="result.manaDep">
						<option value="">管理院系</option>
					</select>
				</div>

			</div>
		</form>
	</div>

</div>